﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MulagoDb.database;
using MulagoDb.model_old;

namespace MulagoDb
{
    public partial class ProblemListPanel : UserControl
    {
        public ProblemListPanel()
        {
            InitializeComponent();
        }

        private void ProblemListPanel_Load(object sender, EventArgs e)
        {
            //note: the top table has a list of recorded problems
            grid1.BorderStyle = BorderStyle.FixedSingle;
            grid1.ColumnsCount = 4;
            grid1.FixedRows = 1;
            grid1.Rows.Insert(0);
            grid1[0, 0] = new SourceGrid.Cells.ColumnHeader("Problem");
            grid1[0, 0].Column.Width = 300;
            grid1[0, 1] = new SourceGrid.Cells.ColumnHeader("Date of Onset");
            grid1[0, 1].Column.Width = 200;
            grid1[0, 2] = new SourceGrid.Cells.ColumnHeader("Date Of Resolution");
            grid1[0, 2].Column.Width = 200;
            grid1[0, 3] = new SourceGrid.Cells.ColumnHeader("Comments");
            grid1[0, 3].Column.Width = 200;
            addRowToGrid(1);
            addRowToGrid(2);
            addRowToGrid(3);
            addRowToGrid(4);

            grid1.Width = 910;


            //fill grid with values
            setSingleProbroblems(ProblemList.getProblemList(FindPatient.patientId));

        }

        public int addRowToGrid(int row)
        {
            int rowNum = 0;
            grid1.Rows.Insert(row);
            grid1[row, 0] = new SourceGrid.Cells.Cell("", typeof(string));
            grid1[row, 1] = new SourceGrid.Cells.Cell("", typeof(string));
            grid1[row, 2] = new SourceGrid.Cells.Cell("", typeof(string));
            grid1[row, 3] = new SourceGrid.Cells.Cell("", typeof(string));
            grid1[row, 0].Row.Height = 30;
            rowNum = row;
            return rowNum;

        }
        private void grid1_Paint(object sender, PaintEventArgs e)
        {

        }
        public StringBuilder getSingleSelect()
        {
            StringBuilder result = new StringBuilder();
            result.Append(g6PdeficiencyCheck.Checked ? "T" : "F");
            result.Append("," + hivInfectionCombo.Text);
            result.Append(TbprobableCheck.Checked ? ",T," : ",F,");
            result.Append(osteomyelitisCheck.Checked ? osteomyelitisTfld.Text : ",F");
            result.Append(documentrecurrentCheck.Checked ? ",T" : ",F");
            result.Append(recurrentHeadacheCheck.Checked ? ",T" : ",F");
            result.Append(asthmacheck.Checked ? ",T" : ",F");
            result.Append(chronichypoxemiacheck.Checked ? ",T" : ",F");
            result.Append(pulmonaryhypertensioncheck.Checked ? ",T" : ",F");
            result.Append(acutecholecystitischeck.Checked ? ",T" : ",F");
            result.Append(subacutechroniccholecheck.Checked ? ",T" : ",F");
            result.Append(acutesplenicsequestrationcheck.Checked ? ",T" : ",F");
            result.Append(subacutechronicsplenomegalycheck.Checked ? ",T" : ",F");
            result.Append(acuteaplasiacheck.Checked ? ",T" : ",F");
            result.Append(hyperhemolysisposttransfusioncheck.Checked ? ",T" : ",F");
            result.Append(hyperhemolysisnotposttransfusioncheck.Checked ? ",T" : ",F");
            result.Append(splenectomycheck.Checked ? ",T" : ",F");
            result.Append(cholecystectonomycheck.Checked ? ",T" : ",F");
            result.Append(tonsillectomycheck.Checked ? ",T" : ",F");
            result.Append(caesariansectioncheck.Checked ? ",T" : ",F");
            result.Append("," + othersTfld.Text);
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string singleSelect = getSingleSelect().ToString();
            string documentsepsis = getTreeNodeState(documented_sepsisTreeview);
            string tcdabnormal = getTreeNodeState(tcdabnormalview);
            string stroke = getTreeNodeState(stroketreeView);
            string retinopathy = getTreeNodeState(retinopathytreeView);
            string avascularnecrosis = getTreeNodeState(avascularnecrosistreeView);
            string legulcer = getTreeNodeState(legulcertreeView);
            string pregnancy = getTreeNodeState(pregnancytreeView);
            string rbcalloimmunisation = getTreeNodeState(rbcalloimmunisationtreeView);
            ProblemList.insetProblemList(FindPatient.patientId, singleSelect, documentsepsis,
                tcdabnormal, stroke, retinopathy, avascularnecrosis, legulcer, pregnancy, rbcalloimmunisation);
            MessageBox.Show(this, "Successfully submitted");
        }

        public string getTreeNodeState(TreeView tree)
        {
            StringBuilder nodeStates = new StringBuilder();
            foreach (TreeNode pnode in tree.Nodes)
            {
                foreach (TreeNode cnode in pnode.Nodes)
                {
                    if (cnode.Checked)
                    {
                        nodeStates.Append("T");
                    }
                    else
                    {
                        nodeStates.Append("F");
                    }
                }
            }
            return nodeStates.ToString();
        }

        public void setSingleProbroblems(ArrayList problem)
        {
            int count = problem.Count;
            int rowNum = 0;
            for (int i = 1; i <= count; ++i)
            {
                string singleSelect = ((Problems)problem[i - 1]).getSingleSelect();
                string[] split = singleSelect.Split(',');
                //add single select problems
                if (split[0].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum+1);
                    grid1[rowNum, 0].Value = g6PdeficiencyCheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }

                rowNum = addRowToGrid(rowNum + 1);
                grid1[rowNum, 0].Value = split[1].ToString();
                grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();

                if (split[2].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = TbprobableCheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[3].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = osteomyelitisTfld.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }



                if (split[4].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = documentrecurrentCheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[5].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = recurrentHeadacheCheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[6].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = asthmacheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[7].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = chronichypoxemiacheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[8].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = pulmonaryhypertensioncheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[9].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = acutecholecystitischeck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[10].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = subacutechroniccholecheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[11].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = acutesplenicsequestrationcheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[12].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = subacutechronicsplenomegalycheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[13].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = acuteaplasiacheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[14].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = hyperhemolysisposttransfusioncheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[15].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = hyperhemolysisnotposttransfusioncheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[16].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = splenectomycheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[17].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = cholecystectonomycheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[18].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = tonsillectomycheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[19].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = caesariansectioncheck.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
                if (split[20].Equals("T"))
                {
                    rowNum = addRowToGrid(rowNum + 1);
                    grid1[rowNum, 0].Value = othersTfld.Text;
                    grid1[rowNum, 1].Value = ((Problems)problem[i - 1]).getDate();
                }
            }



        }
    }
}
